﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BDS3M.Admin.Controls
{
    public partial class EditLogo : System.Web.UI.UserControl
    {
        public string imagelogo;
        public int logoid;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                DataTable dt = DAL.DCollector.DBCollector.Instance.StoredSelectAllTable("tbl_Logo");
                imagelogo = dt.Rows[0]["Logo_Image"].ToString();
                logoid = Convert.ToInt32(dt.Rows[0]["Logo_ID"].ToString());
            }
            catch (Exception)
            {
                Response.Redirect("/error.htm");
            }            
        }

        protected void btnUpdateLogo_Click(object sender, EventArgs e)
        {
         
                if (fulImage.HasFile)
                {
                    if (LIB.Validate.CheckFileType(fulImage.FileName))
                    {
                        string link = fulImage.FileName;
                        LIB.Validate saveimage = new LIB.Validate();
                        saveimage.ThayDoiKichThuocAnh(Server.MapPath("/Icons/"), link, 200, fulImage.PostedFile.InputStream);
                        string filePathLogo = "/Icons/" + fulImage.FileName;

                        DAL.DHelpers.Column[] cl = new DAL.DHelpers.Column[2];
                        cl[0] = new DAL.DHelpers.Column("Logo_Image", filePathLogo, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                        cl[1] = new DAL.DHelpers.Column("Logo_Date", DateTime.Now.ToString(), DAL.DHelpers.DataType.DataTypeSql.Varchar);

                        string[] colWhere = new string[1];
                        colWhere[0] = "Logo_ID";

                        object[] colValue = new object[1];
                        colValue[0] = logoid;

                        DAL.DCollector.DBCollector.Instance.Update("tbl_Logo", cl, colWhere, colValue);
                        Response.Redirect("/Index.aspx?panel=7&sub=0");
                    }
                    else
                    {
                        Response.Write("<script>alert('Chỉ có thể upload file(.jpg,.png,.jpeg)')</script>");
                    }
                }
                else
                {
                    Response.Write("<script>alert('Bạn vui lòng upload ảnh...')</script>");
                }                  
        }
    }
}